<?php
/**
 * User: Nicho
 * Date: 17-3-31
 * Time: AM 08:10
 */
namespace Admin\Controller;
use Think\UploadFile;

class UnitController extends AdminController
{
    //单位表
    private $unitModel;
    //单位配置表
    private $unitConfModel;
    //省市区表
    private $districtModel;
    public function __construct(){
        parent::__construct();
        $this->unitModel = M('core_unit');
        $this->districtModel = M('district');
        $this->unitConfModel = M('core_unit_config');
        $this->unitConfModel->create();
        $this->unitModel->create();
    }
    const MODEL_NAME = 'Unit@:';
    //单位列表
    public function index()
    {
        if(I('keyword')){
            $keyword = I('keyword');
            $map['title'] = array('like','%'.$keyword.'%');
            $this->assign('keyword',$keyword);
        }
        $map['pid'] = 0;
        $map['status'] = array('neq',-1);
        $totalCount = $this->unitModel->where($map)->count();
        $page = new \Think\Page($totalCount,20);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = $this->unitModel->where($map)->limit($limit)->order('sort desc,id desc')->select();
        foreach ($list as $key => $value) {
            $con['id'] = array('in',$value[province].','.$value[city].','.$value[district]);
            $district = $this->districtModel->where($con)->field('name')->select();
            $list[$key]['dname'] = $district;
            $list[$key]['sub'] = $this->unitModel->where('pid = '.$value['id'])->select();
        }
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('sort',$sort);
        $this->meta_title = '单位列表';
        $this->display(self::MODEL_NAME.'Unit/index'); 
        
    }
    //添加/编辑单位
    public function updateUnit()
    {
        if(IS_POST){
            $this->unitModel->provinceID = I('provinceID');
            $this->unitModel->cityID = I('cityID');
            $this->unitModel->areaID = I('areaID');
            switch (I('a_type')) {
                case 'add':
                    $this->unitModel->create_time = time();
                    $result = $this->unitModel->add();
                    break;
                case 'edit':
                    $this->unitModel->update_time = time();
                    $result = $this->unitModel->where('id = '.I('id'))->save();
                    break;
            }
            if($result){
                $this->success('更新成功',U('index'));
            }else{
                $this->error('更新失败');
            }
        }else{
            //所属上级
            $unitList = $this->unitModel->where('status = 1 and pid = 0')->order('id desc')->select();
            $this->assign('unitList',$unitList);

            $unit = $this->unitModel->where('id = '.I('id'))->find();
            $this->assign('data',$unit);
            $this->assign('a_type',I('a_type'));
            $this->assign('pro',AddonsController::pro());
            $this->display(self::MODEL_NAME.'Unit/updateUnit');
        }
    }
    //单位配置
    public function unitConf()
    {
        if(IS_POST){
            if(I('id')){
                $this->unitConfModel->update_time = time();
                $result = $this->unitConfModel->where('id = '.I('id'))->save();
            }else{
                $this->unitConfModel->create_time = time();
                $result = $this->unitConfModel->add();
            }
            if($result){
                $this->success('更新成功',U('index'));
            }else{
                $this->error('更新失败');
            }
        }else{
            //单位列表
            $unitConf = $this->unitConfModel->where('unit = '.I('id'))->find();
            // //缴费项列表
            // $payList = M('core_unit_paytype')->where('status = 1 and unit = '.I('id'))->select();
           
            // $this->assign('payList',$payList);
            $this->assign('data',$unitConf);
            $this->assign('tid',I('id'));
            $this->assign('utitle',I('utitle'));
            $this->display(self::MODEL_NAME.'Unit/unitConf');
        }
    }
  
    //接口类型
    public function type()
    {   
        if(I('keyword')){
            $keyword = I('keyword');
            $map['title'] = array('like','%'.$keyword.'%');
            $this->assign('keyword',$keyword);
        }
        if(I('sort')){
            $sort = I('sort');
            if($sort == 'recycle'){
                $map['status'] = -1;

                $this->assign('recycle','【回收站】');
            }
        }else{
            $sort = 'normal';
            $map['status'] = array('egt', 0);
        }
        $model = D('CoreInterfaceType');
        $totalCount = $model->where($map)->count();
        $page = new \Think\Page($totalCount,10);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = $model->where($map)->limit($limit)->order('sort,id desc')->select();
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('sort',$sort);
        $this->meta_title = '接口类型';
        $this->display(self::MODEL_NAME.'Type/index'); 
        
    }
     public function addType()
    {
        if(IS_POST){
            if(!I('post.title')) $this->error('请输入名称!');
            $model_object = D('CoreInterfaceType');
            $model_object->title = trim(I('post.title'));
            $model_object->remark = trim(I('post.remark'));
            $model_object->status = 1;
            $model_object->create_time = NOW_TIME;
            if($model_object->add()){
                $this->success('新增成功!',U('type'));
            }else{
                $this->error('新增失败!');
            }
        }

        
        $this->meta_title = '新增类型';
        $this->display(self::MODEL_NAME.'Type/addtype');

    }
    public function editType()
    {
        if(IS_POST){
            if(!I('post.title')) $this->error('请输入名称!');
            $id = I('post.id');
            $model_object = D('CoreInterfaceType');
            $model_object->title = trim(I('post.title'));
            $model_object->remark = trim(I('post.remark'));
            $model_object->update_time = NOW_TIME;
            $model_object->id = $id;
            if($model_object->save()){
                $this->success('更新成功!',U('type'));
            }else{
                $this->error('更新失败!');
            }
        }
        $id = I('get.id');
        $model_object = D('CoreInterfaceType');
        $data = $model_object->find($id);
        $this->assign('data',$data);
        $this->meta_title = '编辑类型';
        $this->display(self::MODEL_NAME.'Type/addtype');

    }

    //设置状态
     public function setStatus()
    {
        $ids = I('request.id');
        $model = I('request.model');
        if(!$ids)$this->error('无可操作数据!');
        $map['id'] = array('in',$ids);
        $method = I('request.method');
        $model_object = D($model);
        switch ($method) {
            case 'resume':
                $model_object->status = 1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('激活成功!');
                }else{
                    $this->error('激活失败!');
                }
                break;
            case 'forbid':
                $model_object->status = 2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('禁用成功!');
                }else{
                    $this->error('禁用失败!');
                }
                break;
            case 'delete':
                $model_object->status = -1;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('删除成功!');
                }else{
                    $this->error('删除失败!');
                }
                break;
            case 'recover':
                $model_object->status = 0;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('恢复成功!');
                }else{
                    $this->error('恢复失败!');
                }
                break;
            case 'destory':
                $model_object->status = -2;
                $result = $model_object->where($map)->save();
                if($result){
                    $this->success('彻底删除成功!');
                }else{
                    $this->error('彻底删除失败!');
                }
                break;
            default:
                $this->error('系统参数错误!');
                break;
        }
    }
    public function classlist()
    {
       if(I('keyword')){    //查询关键字进行搜索。
            $keyword = I('keyword');
            $map['title'] = array('like','%'.$keyword.'%');
            $this->assign('keyword',$keyword);
        }
        if(I('sort')){
            $sort = I('sort');
            if($sort == 'recycle'){
                $map['status'] = -1;

                $this->assign('recycle','【回收站】');
            }
        }else{
            $sort = 'normal';
            $map['status'] = array('egt', 0);
        }            
        $map['status'] = array('neq','-1');
        $model = D('core_class'); //存入数据库中，进行排序并显示页面
        $totalCount = $model->where($map)->count();
        $page = new \Think\Page($totalCount,10);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = $model->where($map)->limit($limit)->order('sort asc,id asc')->select();
        foreach ($list as $key => $value) {
            $leave = M('core_unit')->where('id = '.$value['unit'])->getField('title');
            $list[$key]['unit'] = $leave; 
        }
        $this->assign('_list',$list);
        $this->assign('_page',$page->show());
        $this->assign('sort',$sort);
        $this->meta_title = '班级列表';
        $this->display(self::MODEL_NAME.'Unit/classlist'); 
         
    }
     public function addclass()
    {
        if(IS_POST){
            if(!I('post.title')) $this->error('请输入名称!');
            $model_object = D('core_class');
            $model_object->title = trim(I('title'));
            $model_object->unit = trim(I('unit'));
            $model_object->remark = trim(I('remark'));
            $model_object->sort = trim(I('sort'));
            $model_object->status = 1;
            $model_object->create_time = NOW_TIME;
            if($model_object->add()){
                $this->success('新增成功!',U('classlist'));
            }else{
                $this->error('新增失败!');
            }
        }
        $typeList = M('core_unit')->where('status='.'1')->select();
        $this->assign('typeList',$typeList);
        $this->meta_title = '新增类型';
        $this->display(self::MODEL_NAME.'Unit/addclass');
    }
     public function editclass()
    {
        if(IS_POST){
            if(!I('post.title')) $this->error('请输入名称!');
            $id = I('post.id');
            $model_object = D('core_class');
            $model_object->title = trim(I('title'));
            $model_object->unit = trim(I('unit'));
            $model_object->remark = trim(I('remark'));
            $model_object->update_time = NOW_TIME;
            $model_object->id = $id;
            if($model_object->save()){
                $this->success('更新成功!',U('classlist'));
            }else{
                $this->error('更新失败!');
            }
        }
        $ddd=M('core_class')->where('id='.I('id'))->find();
        $this->assign('ddd',$ddd);
        $typeList = M('core_unit')->where('status='.'1')->select();
        $this->assign('typeList',$typeList);
        $this->meta_title = '编辑班级列表';
        $this->display(self::MODEL_NAME.'Unit/editclass');

    }
    public function move()
    {
       
       if(I('keyword')){    //查询关键字进行搜索。
            $keyword = I('keyword');
            $map['nickname|class_title'] = array('like','%'.$keyword.'%');
            $this->assign('keyword',$keyword);
        }
        if(I('sort')){
            $sort = I('sort');
            if($sort == 'recycle'){
                $map['status'] = -1;

                $this->assign('recycle','【回收站】');
            }
        }else{
            $sort = 'normal';
            $map['status'] = array('egt', 0);
        }
        $param = array(
            'keyword'=>I('keyword'),
            'sort'=>I('sort'),
            'status'=>I('status')
            );          
        $model=D('core_userinfo_view');
        $map['m_status'] = array('eq',1);
        $map['group_id'] = 5;
        $totalCount = $model->where($map)->count();
        $page = new \Think\Page($totalCount, 10,$param);
        $limit= $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = $model->where($map)->limit($limit)->order('uid desc')->select();
        $this->assign('_list', $list);
        $this->assign('_page', $page->show());
        $this->assign('sort', $sort);
        $this->meta_title = '用户列表';
        $this->display(self::MODEL_NAME.'Unit/move');

    }
    public function moveClass()
    {
        M('core_user_info')->create(); 
        if(IS_POST){
            if(!I('post.class_id')) $this->error('请选择新班级!');
            if(M('core_user_info')->where('uid = '.I('tid'))->save()){
                $this->success('更新成功!',U('move'));
            }else{
                $this->error('更新失败!');
            }
        }
        $list=M('core_class')->where('status = 1')->select();
        $this->assign('_list',$list);
        $this->assign('tid',I('tid'));
        $this->meta_title = '调班';
        $this->display(self::MODEL_NAME.'Unit/moveClass');
    }
    public function updateClass()
    {
        if(IS_POST){
            $con['unit_id'] = I('unit');
            $con['class_id'] = I('old');
            $con['group_id'] = 5;
            $data['class_id'] = I('new');
            $result = M('core_user_info')->where($con)->save($data);
            if($result){
                $this->success('修改成功',U('move'));
            }
        }else{
            //单位列表
            $unitList = $this->unitModel->where('status = 1')->select();
            $this->assign('unitList',$unitList);
            //班级列表
            $classList = M('core_class')->where('status = 1')->select();
            $this->assign('classList',$classList);

            $this->display(self::MODEL_NAME.'Unit/updateClass');
        }
    }
    public function amountType()
    {
        if(I('keyword')){    //查询关键字进行搜索。
            $keyword = I('keyword');
            $map['nickname|class_title'] = array('like','%'.$keyword.'%');
            $this->assign('keyword',$keyword);
        }
        if(I('sort')){
            $sort = I('sort');
            if($sort == 'recycle'){
                $map['status'] = -1;

                $this->assign('recycle','【回收站】');
            }
        }else{
            $sort = 'normal';
            $map['status'] = array('egt', 0);
        }
        $param = array(
            'keyword'=>I('keyword'),
            'sort'=>I('sort'),
            'status'=>I('status')
            );          
        $model=D('core_unit_paytype');
        $map['status'] = array('neq',-1);
        $totalCount = $model->where($map)->count();
        $page = new \Think\Page($totalCount, 10,$param);
        $limit= $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = $model->where($map)->limit($limit)->order('id desc')->select();
        foreach ($list as $key => $value) {
            $leave = M('core_unit')->where('id = '.$value['unit'])->getField('title');
            $list[$key]['unit'] = $leave; 
        }
        $this->assign('_list', $list);
        $this->assign('_page', $page->show());
        $this->assign('sort', $sort);
        $this->meta_title = '缴费类型';
        $this->display(self::MODEL_NAME.'Unit/amountType');
    }
    public function addamountType()
    {
        if(IS_POST){

            $dd['create_time']=time();
            $dd['mark']=I('mark');
            $dd['setstate']=I('setstate');
            $dd['title']=I('title');
            $dd['unit']=I('unit');
            $dd['amount']=I('amount');        
            $d=M('core_unit_paytype')->add($dd);
            $this->success('添加成功',U('amountType'));
        }else{
            $typeList = M('core_unit')->where('status='.'1')->select();
            $this->assign('typeList',$typeList);
            $this->meta_title = '新增接口';
            $this->display(self::MODEL_NAME.'Unit/addamountType');   
        }
    }
    public function editamountType()
    {
         if(IS_POST){
            $f['updete_time']=time();
            $f['mark']=I('mark');
            $f['setstate']=I('setstate');
            $f['title']=I('title');
            $f['amount']=I('amount');
            $d=M('core_unit_paytype')->where('id='.I('id'))->save($f);
            $this->success('修改成功',U('amountType'));
        }else{
            $ddd=M('core_unit_paytype')->where('id='.I('id'))->find();
            $this->assign('ddd',$ddd);
            $data = M('core_unit_paytype')->where('id='.I('id'))->getField('unit');
            $typeList=M('core_unit')->where('id='.$data)->find();
            $this->assign('typeList',$typeList);
            // $this->meta_title = '新增接口';
            $this->display(self::MODEL_NAME.'Unit/editamountType');
        }
    }
    public function ad()
    {     
    
        $map['status'] = array('neq','-1');
        $model = D('core_unit_ad'); //存入数据库中，进行排序并显示页面
        $totalCount = $model->where($map)->count();
        $page = new \Think\Page($totalCount,10);
        $limit = $page->firstRow . ',' . $page->listRows . ',' . $page->totalRows;
        $list = $model->where($map)->limit($limit)->order('id desc')->select();
        $this->assign('_page', $page->show());
        $this->assign('_list',$list);
        $this->display(self::MODEL_NAME.'Unit/ad');
    }
         public function addad()
    {
        if(IS_POST)
                {
                    $upload = new UploadFile();// 实例化上传类
                    $upload->maxSize  = 3145728 ;// 设置附件上传大小
                    $upload->allowExts  = array('png','jpg','txt');// 设置附件上传类型
                    $upload->savePath =  './Uploads/Picture/'.date('Ymd').'/';// 设置附件上传目录
                    if(!$upload->upload()) {// 上传错误提示错误信息
                        $this->error($upload->getErrorMsg());
                    }else{// 上传成功 获取上传文件信息
                        $info =  $upload->getUploadFileInfo();
                        //dump($info);exit;
                    }
                        $data['uid'] = $_SESSION['onethink_admin']['user_auth']['uid'];
                        // $data['sname'] = $_POST['name1'];
                        // $data['price'] = $_POST['name2'];
                        $data['lujing'] = $info[0]['savepath'].$info[0]['savename'];
                        $data['create_time'] = time();
                        //dump($_SESSION);exit;
                        M('core_unit_ad')->add($data);
                        $this->success("上传成功",U('Unit/ad'));                
                }else
                {
                    $this->display(self::MODEL_NAME.'Unit/addad');
                }
            
    }
}
